// ========================== 
// 描述：
// 作者：MING
// 创建时间：2021/09/02 09:52:07 
// 版本：1.0
// ========================== 

using UIFrame;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;


/// <summary>
/// 热点选择和按钮反应
/// </summary>
public class KeyTab : MonoBehaviour
{
    private static KeyTab instance;
    public Color SelectColor;
    public Color DefaultColor;
    public void Awake()
    {
        if (instance == null)
            instance = this;
        RestAction();
    }
    public static KeyTab Instance { get => instance; }

    public int HotIndex;                                    //热点索引
    private GameObject HotObject;                           //热点     
    private bool NeedChangeColor = true;                    //是否改变热点下面文字的颜色
    private readonly List<CommonItem> ListComItems = new List<CommonItem>();  //热点对象组
    private UnityAction<int> listenAction;                  //热点转移监听事件

    private UnityAction UpAction;
    private UnityAction DownAction;
    private UnityAction EnterAction;
    private UnityAction EscAction;

    #region 注册Common TIP:不需要移除，直接注册新的即可覆盖
    //常规
    public void RegisterCommon<T>(List<T> items) where T : CommonItem
    {
        listenAction = null;

        RestAction();

        ListComItems.Clear();
        ListComItems.AddRange(items.ToArray());

        HotChangeInit();
    }
    //需要返回原热点的
    public void RegisterCommon<T>(List<T> items, int hotIndex, UnityAction<int> listenAction) where T : CommonItem
    {
        this.listenAction = null;

        if (listenAction != null)
            this.listenAction += listenAction;

        RestAction();

        ListComItems.Clear();
        ListComItems.AddRange(items.ToArray());

        HotChangeInit(hotIndex);
    }
    //大杂烩
    public void RegisterCommon<T>(List<T> items, int hotIndex, UnityAction<int> listenAction, bool needChangeColor, bool overrideAction) where T : CommonItem
    {
        NeedChangeColor = needChangeColor;

        this.listenAction = null;

        if (listenAction != null)
            this.listenAction += listenAction;

        if (!overrideAction)
            RestAction();


        ListComItems.Clear();
        ListComItems.AddRange(items.ToArray());

        HotChangeInit(hotIndex);
    }

    //单个常规
    public void RegisterCommon<T>(T item) where T : CommonItem
    {
        listenAction = null;

        RestAction();

        ListComItems.Clear();
        ListComItems.Add(item);

        HotChangeInit();
    }
    //单个大杂烩
    public void RegisterCommon<T>(T item, UnityAction<int> listenAction, bool needChangeColor, bool overrideAction) where T : CommonItem
    {
        NeedChangeColor = needChangeColor;

        this.listenAction = null;
        if (listenAction != null)
            this.listenAction += listenAction;

        if (overrideAction)
            RestAction();

        ListComItems.Clear();
        ListComItems.Add(item);

        HotChangeInit();
    }
    #endregion

    /// <summary>
    /// 热点变更初始化
    /// </summary>
    public void HotChangeInit(int index = 0)
    {
        if (ListComItems.Count == 0)
        {
            GetComponent<Image>().enabled = false;
            return;
        }
        else
        {
            GetComponent<Image>().enabled = true;
        }
        HotIndex = index;
        HotObject = ListComItems[HotIndex].gameObject;
        //更新光标位置和热点显示
        UpdateHotAndSign();
    }

    private void Update()
    {
        KeyDetection();
    }

    /// <summary>
    /// 按键检测
    /// </summary>
    private void KeyDetection()
    {
        if (Input.GetKeyDown(KeyCode.W))
        {
            UpAction?.Invoke();
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
            DownAction?.Invoke();
        }
        if (Input.GetKeyDown(KeyCode.E))
        {
            EnterAction?.Invoke();
        }
        if (Input.GetKeyDown(KeyCode.Q))
        {
            EscAction?.Invoke();
        }

        //if (InputCtrl.GetInstace().GetKeyDown(InputCtrl.KeyCode.GAME_P1_UP))
        //{
        //    UpAction?.Invoke();
        //}
        //if (InputCtrl.GetInstace().GetKeyDown(InputCtrl.KeyCode.GAME_P1_DOWN))
        //{
        //    DownAction?.Invoke();
        //}
        //if (InputCtrl.GetInstace().GetKeyDown(InputCtrl.KeyCode.GAME_P1_ENTER))
        //{
        //    EnterAction?.Invoke();
        //}
        //if (InputCtrl.GetInstace().GetKeyDown(InputCtrl.KeyCode.GAME_P1_ERROR))
        //{
        //    EscAction?.Invoke();
        //}
    }

    #region 自定义按钮事件
    public void RegisterUpAction(UnityAction action)
    {
        UpAction = action;
        UpAction += NoticeAction;
    }
    public void RegisterDownAction(UnityAction action)
    {
        DownAction = action;
        DownAction += NoticeAction;
    }
    public void RegisterEnterAction(UnityAction action)
    {
        EnterAction = action;
    }
    public void RegisterEscAction(UnityAction action)
    {
        EscAction = action;
    }
    #endregion

    #region 按键事件
    /// <summary>
    /// 确认
    /// </summary>
    private void EnterEvent()
    {
        ListComItems[HotIndex].ActivationEvent();
    }
    /// <summary>
    /// 返回
    /// </summary>
    private void EscapeEvent()
    {
        Debug.Log("返回键触发");
    }
    /// <summary>
    /// 下一个（切换）
    /// </summary>
    public void DownEvent()
    {
        HotIndex = ++HotIndex % ListComItems.Count;
        HotObject = ListComItems[HotIndex].gameObject;
        NoticeAction();
    }
    /// <summary>
    /// 上一个（切换）
    /// </summary>
    public void UpEvent()
    {
        if (HotIndex == 0)
            HotIndex = ListComItems.Count - 1;
        else
            HotIndex = --HotIndex % ListComItems.Count;
        HotObject = ListComItems[HotIndex].gameObject;
        NoticeAction();
    }
    /// <summary>
    /// 重置按钮事件
    /// </summary>
    private void RestAction()
    {
        UpAction = UpEvent;
        DownAction = DownEvent;
        EscAction = EscapeEvent;
        EnterAction = EnterEvent;
    }
    #endregion

    #region 切换选项事件
    /// <summary>
    /// 更新热点和光标
    /// </summary>
    private void UpdateHotAndSign()
    {
        //更新光标位置
        Rect hotTransform = HotObject.GetComponent<RectTransform>().rect;
        gameObject.transform.position = HotObject.transform.position + new Vector3(-hotTransform.width / 2, 0, 0);
        if (NeedChangeColor)
        {
            //更新热点文本颜色
            foreach (var item in ListComItems)
            {
                if (HotObject == item.gameObject)
                    item.GetComponentInChildren<Text>().color = SelectColor;
                else
                    item.GetComponentInChildren<Text>().color = Color.black;
            }
        }
    }
    private void NoticeAction()
    {
        //更新光标位置和热点显示
        UpdateHotAndSign();
        listenAction?.Invoke(HotIndex);
    }
    #endregion
}